package cc.facesign.admin.controller.front;

import cc.facesign.admin.bean.fv.ConfInfoFv;
import cc.facesign.admin.bean.fv.ConfSimpleSignFv;
import cc.facesign.admin.bean.fv.UserInfoFv;
import cc.facesign.admin.bean.fv.UserSimpleSignFv;
import cc.facesign.admin.bean.vo.ConfVo;
import cc.facesign.admin.bean.vo.UserVo;
import cc.facesign.admin.service.SignService;
import cc.facesign.admin.service.TblConfService;
import cc.facesign.admin.service.TblConfUserService;
import cc.facesign.admin.service.TblUserService;
import cc.facesign.admin.utils.MqUtils;
import cc.facesign.common.utils.bean.R;
import cc.facesign.common.utils.bean.XhttpR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(description = "前台会议接口")
@RestController
@RequestMapping("/front/conf")
public class FrontConfController {

    @Autowired
    private TblConfService confService;

    @Autowired
    private TblConfUserService confUserService;

    @Autowired
    private TblUserService userService;

    @Autowired
    private SignService signService;

    @Autowired
    private MqUtils mqUtils;

    @ApiOperation("按会议码查会议详情")
    @GetMapping("/getInfoByCode/{code}")
    public Object getInfoByCode(@PathVariable String code) {
        // TODO 权限待加

        // 获取会议信息
        ConfVo conf = confService.getInfoByCode(code);

        // 获取会议中没有签到的用户id
        Set<String> set = confService.getNoSignUserSetByConfId(conf.getId());

        // 获取会议中的用户信息
        List<UserVo> userList = confService.getUserListByConfId(conf.getId());
        for (UserVo e : userList) {
            e.setPassword(null);
            e.setIsSign(!set.contains(e.getId()) ? 1 : 0);
        }

        ConfInfoFv res = new ConfInfoFv();
        BeanUtils.copyProperties(conf, res);
        res.setUserInfo(new ArrayList<>());
        for (UserVo vo : userList) {
            UserInfoFv t = new UserInfoFv();
            BeanUtils.copyProperties(vo, t);
            res.getUserInfo().add(t);
        }

        return XhttpR.data(res);
    }

    @ApiOperation("按会议码查会议详情（simple）")
    @GetMapping("/getSimpleInfoByCode/{code}")
    public Object getSimpleInfoByCode(@PathVariable String code) {
        // TODO 权限待加
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);

        ConfVo conf = confService.getInfoByCode(code);

        ConfSimpleSignFv res = new ConfSimpleSignFv();
        res.setCode(code);
        res.setUsers(new ArrayList<>());
        for (UserVo e : confService.getUserInfoContainsSignByConfId(conf.getId())) {
            UserSimpleSignFv fv = new UserSimpleSignFv();
            BeanUtils.copyProperties(e, fv);
            if (fv.getIsSign() == 2) fv.setIsSign(1);
            res.getUsers().add(fv);
        }

        return XhttpR.data(res.getUsers());
    }

    /**
     * 按query中的会议id、用户cardId修改
     * @return
     */
    @ApiOperation("修改签到信息")
    @PutMapping("/modifySign/{mid}/{cardId}")
    public R modify(@PathVariable("mid") String mid,
                    @PathVariable("cardId") String cardId,
                    @RequestBody Map<String, String> map) {

        String status = map.get("isSign");

        signService.sign(mid, cardId, Integer.parseInt(status));

        return R.ok();
    }
}
